import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:english_words/english_words.dart';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http; //导入网络请求相关的包
class ListTestPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ListTestPageState();
  }
}

class ListTestPageState extends State<ListTestPage> {
  final _suggestions = <WordPair>[];
  final _saved = new Set<WordPair>();
  final _biggerFont = const TextStyle(fontSize: 18.0);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(nativeMessage),
          actions: <Widget>[
            new IconButton(icon: new Icon(Icons.list), onPressed: _pushSaved),
            new IconButton(icon: new Icon(Icons.map), onPressed: doNativeSuff),
            new IconButton(icon: new Icon(Icons.add), onPressed: _pullNet),
          ],
        ),
        body: _buildSuggestions());
  }

  Widget _buildSuggestions() {
    return new ListView.builder(
      itemBuilder: (context, i) {
        if (i.isOdd) {
          return new Divider(color: Colors.red,
          height: 10,);
        }
        final index = i ~/ 2;
        if (index >= _suggestions.length) {
          _suggestions.addAll(generateWordPairs().take(2));
        }
        return _buildRow(_suggestions[index]);
      },
      padding: const EdgeInsets.all(16.0),
    );
  }

  Widget _buildRow(WordPair pair) {
    final alreadySaved = _saved.contains(pair);

    return new ListTile(
      subtitle: new Text("subtitle"),
      title: new Text(
        pair.asPascalCase,
        style: _biggerFont,
      ),
      trailing: new Icon(
        alreadySaved ? Icons.favorite : Icons.favorite_border,
        color: alreadySaved ? Colors.red : null,
      ),
      onTap: () {
        setState(() {
          if (alreadySaved) {
            _saved.remove(pair);
          } else {
            _saved.add(pair);
          }
        });
      },
      onLongPress: (){
       showDialog(context: context,);
      },
    );
  }

  void _pushSaved() {
    Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
      final tiles = _saved.map((pair) {
        return new ListTile(
          title: new Text(pair.asPascalCase, style: _biggerFont),
        );
      });
      final divided = ListTile.divideTiles(
        context: context,
        tiles: tiles,
      ).toList();

      return new Scaffold(
        appBar: new AppBar(),
        body: new ListView(
          children: divided,
        ),
      );
    }));
  }
  void _delayPushSaved(){


    getDatas().then((data){
      print(data);
    }).catchError((e){
      print(e);
    });

  }
  static const platformMethodChannel = const MethodChannel('sample.flutter.io/pull');

  String nativeMessage ='XXXXX';
  @override
  Future<Null> doNativeSuff() async {
    String _message; // 1
    try {
      final String result =
      await platformMethodChannel.invokeMethod('changeLife');// 2
      _message = result;
      print(result);
    } on PlatformException catch (e) {
      _message = "Sadly I can not change your life: ${e.message}.";
    }
    setState(() {
      nativeMessage = _message; // 3
    });
  }


  Future<String> getDatas(){
    return new Future<String>.delayed(new Duration(milliseconds: 2000),(){


      _pushSaved();
      return _saved.toString();
    });
  }
  void _pullNet()  {
    http.get("https://v.qq.com/x/page/y0540bq0nno.html")
        .then((http.Response response) {
      var convertDataToJson = json.decode(response.body);
      convertDataToJson = convertDataToJson["data"]["datas"];
      //打印请求的结果
      print(convertDataToJson);
      //更新数据
      setState(() {

        //data = convertDataToJson;
      });
    });
  }

}
